﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Services.Client;
using System.IO;
using VsxFactory.Modeling.Repository;

namespace VsxFactory.Modeling.Repository
{
    /// <summary>
    /// There are no comments for RepositoryDataContext in the schema.
    /// </summary>
    public partial class RepositoryDataContext : global::System.Data.Services.Client.DataServiceContext
    {
        /// <summary>
        /// Initialize a new RepositoryDataContext object.
        /// </summary>
        public RepositoryDataContext(global::System.Uri serviceRoot) : base(new Uri( serviceRoot + "/Strategies.svc"))
        {
            this.ResolveName = new global::System.Func<global::System.Type, string>(this.ResolveNameFromType);
            this.ResolveType = new global::System.Func<string, global::System.Type>(this.ResolveTypeFromName);
            this.OnContextCreated();
        }
        partial void OnContextCreated();

        protected global::System.Type ResolveTypeFromName(string typeName)
        {
                return this.GetType().Assembly.GetType(typeName, false);
        }
        
        protected string ResolveNameFromType(global::System.Type clientType)
        {
            return clientType.FullName;
        }

        //public IEnumerable<PackageInfo> GetDependentPackages(string packageID, string version, string platform="")
        //{
        //    //return base.CreateQuery<PackageInfo>("GetDependentPackages").AddQueryOption("packageID", packageID).AddQueryOption("version", version).AddQueryOption("platform", platform);
        //    return base.Execute<PackageInfo>( 
        //        new Uri(String.Format("GetDependentPackages?packageID='{1}'&version='{1}'&platform='{2}", packageID, version, platform), 
        //            UriKind.Relative));
        //}

        /// <summary>
        /// There are no comments for Packages in the schema.
        /// </summary>
        public global::System.Data.Services.Client.DataServiceQuery<PackageInfo> Packages
        {
            get
            {
                if ((this._Packages == null))
                {
                    this._Packages = base.CreateQuery<PackageInfo>("Packages");
                }
                return this._Packages;
            }
        }
 
        //public PackageInfo FindPackage(string id, string version, string platform)
        //{
        //    return Packages.Where(p => p.Id == id && p.Version == version).FirstOrDefault();
        //}

        private global::System.Data.Services.Client.DataServiceQuery<PackageInfo> _Packages;

        public void AddToPackages(PackageInfo packageInfo)
        {
            System.Diagnostics.Contracts.Contract.Requires(packageInfo != null);

            base.AddObject("Packages", packageInfo);
        }

        public void SetSaveStream(PackageInfo packageInfo, Stream stream)
        {
            SetSaveStream(packageInfo, stream, true, "application/octet-stream", packageInfo.CreateSlug());
        }
    }

}
